import { View } from "../../../mvc/core/View";
import { Cache } from "../../cache/Cache";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { ELottertType } from "../../../../Message/Public/ELottertType";
import { Language } from "../../../../extend/language/Language";
import { ItemData } from "../../resource/info/ItemData";
import { SelectNumWindow } from "../common/SelectNumWindow";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { LoaderManager } from "../../../../com/gengine/resource/LoaderManager";
import { MsgManager } from "../../manager/MsgManager";
import { Alert } from "../../../../com/mui/controls/Alert";
import { SWFInfo } from "../../../../com/gengine/resource/info/SWFInfo";
import { ExplorerConfig } from "../../resource/ExplorerConfig";
import { EPrictUnit } from "../../../../Message/Public/EPrictUnit";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { ExplorerItemConfig } from "../../resource/ExplorerItemConfig";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { TilePanel } from "../equipment/TilePanel";
import { TreasureShowItemRenderer } from "./TreasureShowItemRenderer";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { GTextFormat } from "../../../common/GTextFormat";
import { EActiveType } from "../../../../Message/Public/EActiveType";
import { ObjectItem } from "./ObjectItem";
import { GTextFiled } from "../../../../com/mui/controls/GTextFiled";
import { _itemSeabedArr } from "../../../../this/_itemSeabedArr";
import { _itemHeavenArr } from "../../../../this/_itemHeavenArr";
import { _itemKunlunArr } from "../../../../this/_itemKunlunArr";
type int = number;
//class TreasureTypePanel
    
    export  class TreasureTypePanel extends View
    {
        constructor()
        {
            super();
            this._btnArr = [];
            this._textArr = [];
            this._taobaoNumArr = [0, 0, 0];
            this._taobaoUnitArr = [2, 2, 2];
            this._sItemCodeArr = [];
            this._bItemCodeArr = [];
            this._goldArr = [10, 95, 450];
            this._itemSeabedArr = [];
            this._itemHeavenArr = [];
            this._itemKunlunArr = [];
            this.init();
            return;
        }

        public updateHeavenPropNum(): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            this._itemHeavenArr = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isLotteryHeavenKey);
            if (this._type == ELottertType._ELotteryHeaven) 
            {
                loc3 = 0;
                while (loc3 < this._itemHeavenArr.length) 
                {
                    loc1 = this._itemHeavenArr[loc3];
                    loc2 = loc2 + loc1.itemAmount;
                    ++loc3;
                }
                this._tips1.text = Language.getStringByParam(35023, loc2);
                this._tips2.text = Language.getString(35027);
            }
            return;
        }

        /* internal  */showSelectNumWindow(arg1: ItemData, arg2: int): void
        {
            if (this._selectNumWindow == null) 
            {
                this._selectNumWindow = new SelectNumWindow();
            }
            this._selectNumWindow.updateByItemData(arg1, this.onSelectNumOkHandler);
            this._selectNumWindow.show();
            this._selectNumWindow.maxAmount = arg2 > 99 ? 99 : arg2;
            var loc1=this.localToGlobal(new Object /* flash.geom.Point */(mouseX, mouseY));
            this._selectNumWindow.x = loc1.x - this._selectNumWindow.btnOK.x;
            this._selectNumWindow.y = loc1.y - this._selectNumWindow.btnOK.y;
            return;
        }

        /* internal  */onSelectNumOkHandler(arg1: ItemData, arg2: int): void
        {
            if (arg1 == null) 
            {
                return;
            }
            if (Cache.instance.role.isHideFairyland) 
            {
                this.playerMovieOnlyUseKey();
            }
            else 
            {
                this.playerMovie(0);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.TreasureUseProp, {"itemData": arg1, "amount": arg2}, true));
            return;
        }

        /* internal  */onTaobaoOne2ClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._itemSeabedArr = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isLotteryKey);
            this._itemHeavenArr = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isLotteryHeavenKey);
            this._itemKunlunArr = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isLotteryKunlunKey);
            var loc1=0;
            if (this._itemSeabedArr.length > 0 && this._type == ELottertType._ELotterySeabed) 
            {
                loc1 = this.getPropNum(this._itemSeabedArr);
                if (loc1 > 1) 
                {
                    this.showSelectNumWindow(this._itemSeabedArr[0], loc1);
                }
                else 
                {
                    this.playerMovieOnlyUseKey();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TreasureUseProp, {"itemData": _itemSeabedArr[0], "amount": 1}, true));
                }
            }
            else if (this._itemHeavenArr.length > 0 && this._type == ELottertType._ELotteryHeaven) 
            {
                loc1 = this.getPropNum(this._itemHeavenArr);
                if (loc1 > 1) 
                {
                    this.showSelectNumWindow(this._itemHeavenArr[0], loc1);
                }
                else 
                {
                    this.playerMovieOnlyUseKey();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TreasureUseProp, {"itemData": _itemHeavenArr[0], "amount": 1}, true));
                }
            }
            else if (this._itemHeavenArr.length > 0 && this._type == ELottertType._ELotteryKunlun) 
            {
                loc1 = this.getPropNum(this._itemKunlunArr);
                if (loc1 > 1) 
                {
                    this.showSelectNumWindow(this._itemKunlunArr[0], loc1);
                }
                else 
                {
                    this.playerMovieOnlyUseKey();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TreasureUseProp, {"itemData": _itemKunlunArr[0], "amount": 1}, true));
                }
            }
            return;
        }

        /* internal  */getPropNum(arg1: Array<any>): int
        {
            var loc2=null;
            var loc1=0;
            var loc3=0;
            while (loc3 < arg1.length) 
            {
                loc2 = arg1[loc3];
                if (loc2) 
                {
                    loc1 = loc1 + loc2.itemAmount;
                }
                ++loc3;
            }
            return loc1;
        }

        /* internal  */playerMovieOnlyUseKey(): void
        {
            if (this._itemSeabedArr.length > 0 && this._type == ELottertType._ELotterySeabed || this._itemHeavenArr.length > 0 && this._type == ELottertType._ELotteryHeaven || this._itemHeavenArr.length > 0 && this._type == ELottertType._ELotteryKunlun) 
            {
                if (this._mc != null) 
                {
                    if (!this._isPlaying) 
                    {
                        this._isPlaying = true;
                        addChild(this._mc);
                        this._mc.gotoAndStop(1);
                        this._mc.play();
                        this._mc.addEventListener(flash.events.Event.ENTER_FRAME, this.onEnterFrameHandler);
                    }
                }
                else 
                {
                    LoaderManager.instance.load("treasureflash.swf", this.onLoaded);
                }
            }
            return;
        }

        /* internal  */onTaobaoTenClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            MsgManager.showRollTipsMsg("该功能已暂时关闭，请到商城购买对应道具");
            return;
        }

        /* internal  */onTaobaoFiftyClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            MsgManager.showRollTipsMsg("该功能已暂时关闭，请到商城购买对应道具");
            return;
        }

        /* internal  */playerMovie(arg1: int): void
        {
            var type: int;
            var onClickHandler: Function;

            var loc1;
            type = arg1;
            if (Cache.instance.role.money.gold >= this._goldArr[type] || this._itemSeabedArr.length > 0 && this._type == ELottertType._ELotterySeabed || this._itemHeavenArr.length > 0 && this._type == ELottertType._ELotteryHeaven || this._itemHeavenArr.length > 0 && this._type == ELottertType._ELotteryKunlun) 
            {
                if (this._mc != null) 
                {
                    if (!this._isPlaying) 
                    {
                        this._isPlaying = true;
                        addChild(this._mc);
                        this._mc.gotoAndStop(1);
                        this._mc.play();
                        this._mc.addEventListener(flash.events.Event.ENTER_FRAME, this.onEnterFrameHandler);
                    }
                }
                else 
                {
                    LoaderManager.instance.load("treasureflash.swf", this.onLoaded);
                }
            }
            else 
            {
                onClickHandler = (arg1: int): void
                {
                    if (arg1 == Alert.OK) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.GotoPay));
                        Alert.resetToDefault();
                    }
                    return;
                }
                Alert.show(Language.getString(30103), null, Alert.OK | Alert.CANCEL, null, onClickHandler);
            }
            return;
        }

        /* internal  */onLoaded(arg1: SWFInfo): void
        {
            this._isPlaying = true;
            this._mc = arg1.clip;
            this._mc.x = 15;
            this._mc.y = -85;
            addChild(this._mc);
            this._mc.addEventListener(flash.events.Event.ENTER_FRAME, this.onEnterFrameHandler);
            this._mc.play();
            return;
        }

        /* internal  */onEnterFrameHandler(arg1: Object /* flash.events.Event */): void
        {
            if (this._mc.currentFrame == this._mc.totalFrames) 
            {
                this._isPlaying = false;
                this._mc.stop();
                this._mc.removeEventListener(flash.events.Event.ENTER_FRAME, this.onEnterFrameHandler);
                removeChild(this._mc);
            }
            return;
        }

        public /* override */ update(arg1: Object, ... rest): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            this._type = arg1 as int;
            var loc1=ExplorerConfig.instance.getExplorerInfoByType(this._type);
            var loc5="";
            var loc6=0;
            while (loc6 < loc1.length) 
            {
                loc2 = loc1[loc6];
                loc3 = this._btnArr[loc6];
                loc4 = this._textArr[loc6];
                loc3.label = Language.getStringByParam(35009, loc2.amount);
                if (loc2.unit != EPrictUnit._EPriceUnitGold) 
                {
                    loc5 = Language.getString(31036);
                }
                else 
                {
                    loc5 = Language.getString(31037);
                }
                if (loc2.amount < 10) 
                {
                    loc4.htmlText = "【" + loc2.cost + loc5 + "】";
                }
                else 
                {
                    loc4.htmlText = "【" + loc2.cost + loc5 + "】";
                }
                loc4.setTextFormat(GlobalStyle.textFormat2);
                this._taobaoNumArr[loc6] = loc2.amount;
                this._taobaoUnitArr[loc6] = loc2.unit;
                this._goldArr[loc6] = loc2.cost;
                ++loc6;
            }
            if (this._type != ELottertType._ELotterySeabed) 
            {
                if (this._type != ELottertType._ELotteryHeaven) 
                {
                    if (this._type == ELottertType._ELotteryKunlun) 
                    {
                        this.updateKunlunPropNum();
                    }
                }
                else 
                {
                    this.updateHeavenPropNum();
                }
            }
            else 
            {
                this.updateSeabedPropNum();
            }
            this._sItemCodeArr = ExplorerItemConfig.instance.getSmallItemInfoByType(this._type);
            this._sItemCodeArr.splice(7, 0, null, null, null, null);
            this._sItemCodeArr.splice(13, 0, null, null, null, null);
            this.updateItems(this._sItemCodeArr);
            this._bItemCodeArr = ExplorerItemConfig.instance.getBigItemInfoByType(this._type);
            this._bItemListTile.dataProvider = new DataProvider(this._bItemCodeArr);
            if (this._selectNumWindow && !this._selectNumWindow.isHide) 
            {
                this._selectNumWindow.hide();
            }
            return;
        }

        public updateSeabedPropNum(): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            this._itemSeabedArr = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isLotteryKey);
            if (this._type == ELottertType._ELotterySeabed) 
            {
                loc3 = 0;
                while (loc3 < this._itemSeabedArr.length) 
                {
                    loc1 = this._itemSeabedArr[loc3];
                    loc2 = loc2 + loc1.itemAmount;
                    ++loc3;
                }
                this._tips1.text = Language.getStringByParam(35008, loc2);
                this._tips2.text = Language.getString(35026);
            }
            return;
        }

        public updateKunlunPropNum(): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            this._itemKunlunArr = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isLotteryKunlunKey);
            if (this._type == ELottertType._ELotteryKunlun) 
            {
                loc3 = 0;
                while (loc3 < this._itemKunlunArr.length) 
                {
                    loc1 = this._itemKunlunArr[loc3];
                    loc2 = loc2 + loc1.itemAmount;
                    ++loc3;
                }
                this._tips1.text = Language.getStringByParam(35033, loc2);
                this._tips2.text = Language.getString(35034);
            }
            return;
        }

        /* internal  */init(): void
        {
            this._bItemListTile = new TilePanel();
            this._bItemListTile.horizontalGap = 95;
            this._bItemListTile.columnWidth = 97;
            this._bItemListTile.rowHeight = 97;
            this._bItemListTile.columnCount = 2;
            this._bItemListTile.rowCount = 1;
            this._bItemListTile.setSize(320, 100);
            this._bItemListTile.setStyle("cellRenderer", TreasureShowItemRenderer);
            this._bItemListTile.move(70, 40);
            addChild(this._bItemListTile);
            this._openSprite = new Object /* flash.display.Sprite */();
            this._openSprite.x = 0;
            this._openSprite.y = 205;
            addChild(this._openSprite);
            var loc1=ObjCreate.createGButton(Language.getStringByParam(35007, 1), 60, 0, 85, 28, this._openSprite, "ButtonSpecial");
            loc1.addEventListener(flash.events.MouseEvent.CLICK, this.onTaobaoOneClickHandler);
            var loc2=ObjCreate.createGButton(Language.getStringByParam(35007, 10), 175, 0, 85, 28, this._openSprite, "ButtonSpecial");
            loc2.addEventListener(flash.events.MouseEvent.CLICK, this.onTaobaoTenClickHandler);
            var loc3=ObjCreate.createGButton(Language.getStringByParam(35007, 50), 290, 0, 85, 28, this._openSprite, "ButtonSpecial");
            loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onTaobaoFiftyClickHandler);
            this._btnArr.push(loc1);
            this._btnArr.push(loc2);
            this._btnArr.push(loc3);
            var loc4=ObjCreate.createTextField("", 70, 35, 150, 20, this._openSprite, GlobalStyle.textFormat2);
            var loc5=ObjCreate.createTextField("", 185, 35, 70, 20, this._openSprite, GlobalStyle.textFormat2);
            var loc6=ObjCreate.createTextField("", 300, 35, 70, 20, this._openSprite, GlobalStyle.textFormat2);
            loc4.visible = false;
            loc5.visible = false;
            loc6.visible = false;
            this._textArr.push(loc4);
            this._textArr.push(loc5);
            this._textArr.push(loc6);
            this._fire = ObjCreate.createBitmap(ImagesConst.Fire, 25, 43, this._openSprite);
            this._tips1 = ObjCreate.createTextField("", 45, 50, 200, 20, this._openSprite, new GTextFormat("", 12, 65280));
            this._closeSprite = new Object /* flash.display.Sprite */();
            this._closeSprite.x = 0;
            this._closeSprite.y = 205;
            addChild(this._closeSprite);
            var loc7;
            (loc7 = ObjCreate.createGButton(Language.getStringByParam(35007, 1), 175, 0, 85, 28, this._closeSprite, "ButtonSpecial")).addEventListener(flash.events.MouseEvent.CLICK, this.onTaobaoOne2ClickHandler);
            this._tips2 = ObjCreate.createGTextField("", 0, 35, 435, 20, this._closeSprite, GlobalStyle.getTextFormatCenter(new GTextFormat("", 12, 16776960)));
            return;
        }

        /* internal  */createTextfield(arg1: int, arg2: int, arg3: int, arg4: int, arg5: string, arg6: Object /* flash.text.TextFormat */=null): Object /* flash.text.TextField */
        {
            var loc1=null;
            (loc1 = new Object /* flash.text.TextField */()).x = arg1;
            loc1.y = arg2;
            loc1.width = arg3;
            loc1.height = arg4;
            if (arg6 != null) 
            {
                loc1.defaultTextFormat = arg6;
            }
            loc1.htmlText = arg5;
            loc1.selectable = false;
            addChild(loc1);
            return loc1;
        }

        /* internal  */updateItemListTile(arg1: TilePanel, arg2: Array<any>): void
        {
            var loc1=new DataProvider();
            var loc2=0;
            while (loc2 < arg2.length) 
            {
                loc1.addItem(arg2[loc2]);
                ++loc2;
            }
            arg1.dataProvider = loc1;
            return;
        }

        /* internal  */updateItems(arg1: Array<any>): void
        {
            var loc4=null;
            var loc6=null;
            var loc1=Cache.instance.active.isActiveInEffect(EActiveType._EActiveTypeNewExploreItem);
            if (this._sprite == null) 
            {
                this._sprite = new Object /* flash.display.Sprite */();
                addChild(this._sprite);
            }
            while (this._sprite.numChildren > 0) 
            {
                loc6 = this._sprite.removeChildAt(0);
                loc6 = null;
            }
            var loc2=28;
            var loc3=0;
            var loc5=0;
            while (loc5 < arg1.length) 
            {
                if (arg1[loc5] != null) 
                {
                    (loc4 = new ObjectItem()).setSize(33, 33);
                    loc4.move(loc2, loc3);
                    if (loc1) 
                    {
                        loc4.itemData = new ItemData(arg1[loc5].activeItem);
                    }
                    else 
                    {
                        loc4.itemData = new ItemData(arg1[loc5].item);
                    }
                    addChild(loc4);
                }
                loc2 = loc2 + 36 + 35;
                if ((loc5 + 1) % 6 == 0) 
                {
                    loc2 = 28;
                    loc3 = loc3 + 50;
                }
                ++loc5;
            }
            return;
        }

        public changeByOpen(): void
        {
            if (Cache.instance.role.isHideFairyland) 
            {
                this._openSprite.visible = false;
                this._closeSprite.visible = true;
            }
            else 
            {
                this._closeSprite.visible = false;
                this._openSprite.visible = true;
            }
            return;
        }

        /* internal  */onTaobaoOneClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._itemSeabedArr = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isLotteryKey);
            this._itemHeavenArr = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isLotteryHeavenKey);
            this._itemKunlunArr = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isLotteryKunlunKey);
            var loc1=0;
            if (this._itemSeabedArr.length > 0 && this._type == ELottertType._ELotterySeabed) 
            {
                loc1 = this.getPropNum(this._itemSeabedArr);
                if (loc1 > 1) 
                {
                    this.showSelectNumWindow(this._itemSeabedArr[0], loc1);
                }
                else 
                {
                    this.playerMovie(0);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TreasureUseProp, {"itemData": _itemSeabedArr[0], "amount": 1}, true));
                }
            }
            else if (this._itemHeavenArr.length > 0 && this._type == ELottertType._ELotteryHeaven) 
            {
                loc1 = this.getPropNum(this._itemHeavenArr);
                if (loc1 > 1) 
                {
                    this.showSelectNumWindow(this._itemHeavenArr[0], loc1);
                }
                else 
                {
                    this.playerMovie(0);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TreasureUseProp, {"itemData": _itemHeavenArr[0], "amount": 1}, true));
                }
            }
            else if (this._itemKunlunArr.length > 0 && this._type == ELottertType._ELotteryKunlun) 
            {
                loc1 = this.getPropNum(this._itemKunlunArr);
                if (loc1 > 1) 
                {
                    this.showSelectNumWindow(this._itemKunlunArr[0], loc1);
                }
                else 
                {
                    this.playerMovie(0);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TreasureUseProp, {"itemData": _itemKunlunArr[0], "amount": 1}, true));
                }
            }
            else 
            {
                MsgManager.showRollTipsMsg("道具不足，请到商城购买对应道具");
            }
            return;
        }

        /* internal */ /* var */_bItemListTile: TilePanel;

        /* internal */ /* var */_type: int;

        /* internal */ /* var */_btnArr: Array<any>;

        /* internal */ /* var */_textArr: Array<any>;

        /* internal */ /* var */_taobaoUnitArr: Array<any>;

        /* internal */ /* var */_sItemCodeArr: Array<any>;

        /* internal */ /* var */_bItemCodeArr: Array<any>;

        /* internal */ /* var */_goldArr: Array<any>;

        /* internal */ /* var */_tips1: Object /* flash.text.TextField */;

        /* internal */ /* var */_fire: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_openSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_closeSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_tips2: GTextFiled;

        /* internal */ /* var */_itemSeabedArr: Array<any>;

        /* internal */ /* var */_itemHeavenArr: Array<any>;

        /* internal */ /* var */_itemKunlunArr: Array<any>;

        /* internal */ /* var */_isPlaying: Boolean=false;

        /* internal */ /* var */_selectNumWindow: SelectNumWindow;

        /* internal */ /* var */_sprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_mc: Object /* flash.display.MovieClip */;

        /* internal */ /* var */_taobaoNumArr: Array<any>;
    }
